package com.kingyun.gpsinspection.purificationservice.services.som.innerservice.impl;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.AccountMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.ConversationMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.som.SomFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Account;
import com.kingyun.gpsinspection.purificationservice.facade.model.som.Conversation;
import com.kingyun.gpsinspection.purificationservice.facade.service.som.SomExpertService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 专家service实现类
 * Created by liuyj on 2017/1/4.
 */
public class SomExpertServiceImpl implements SomExpertService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ConversationMapper conversationMapper;
    @Autowired
    private SomFileMapper somFileMapper;

    /**
     * 获取所有专家
     *
     * @return
     */
    @Override
    public List<Account> findAllExper(Account account) {
        // 设置分页参数limit
        account.setLimit(account.getLimit() == null ? 10 : account.getLimit());
        // 设置分页参数offset
        account.setOffset(account.getOffset() == null ? 0 : account.getOffset());
        return accountMapper.selectAllAcount(account);
    }

    /**
     * 发送消息
     *
     * @return
     */
    @Override
    public int insertConversation(String expertId, String userId, String message, int messageType, int audioTime) {
        Conversation conversation = new Conversation();
        conversation.setFromUserId(userId);
        conversation.setToUserId(expertId);
        conversation.setExpertId(expertId);
        conversation.setEmployeeId(userId);
        if (messageType == 1) {
            conversation.setMessage(message.substring(0, message.indexOf(".png")) + ".jpg");
        } else if (messageType == 2) {
            conversation.setMessage(message.substring(0, message.indexOf(".png")) + ".aac");
        } else {
            conversation.setMessage(message);
        }
        conversation.setMessageType(messageType);
        conversation.setAudioTime(audioTime);
        conversation.setConversationId(UniqueUtil.uuid());
        if (messageType > 0) {
            File file = new File();
            file.setBusinessId(conversation.getConversationId());
            file.setFileHbasePath(conversation.getMessage());
            file.setFileId(UniqueUtil.uuid());
            somFileMapper.insert(file);
        }
        return conversationMapper.insert(conversation);
    }

    /**
     * 获取消息
     *
     * @param userId 用户id
     * @param status 状态0未读，1已经度，2所有
     * @return
     */
    @Override
    public List<Conversation> queryConversations(String userId, int status) {
        Map map = new HashMap<>();
        map.put("userId", userId);
        map.put("status", status);
        List<Conversation> msgs = conversationMapper.queryConversations(map);
        // 如果获取未读或全部，则获取后消息变为已读
        if (status == 0 || status == 2) {
            conversationMapper.readConversatrion(userId);
        }
        return msgs;
    }
}
